package com.sghd.candy.module.shop.manager;

import com.sghd.common.ramcache.IEntity;
import com.sghd.common.ramcache.PerPersist;
import com.sghd.common.ramcache.anno.*;
import com.sghd.common.utils.json.JsonUtils;
import com.sghd.pojo.candy.shop.enums.ConsumeType;
import com.sghd.pojo.candy.shop.enums.ShopModule;

import javax.persistence.*;
import java.util.Map;

/**
 * @author Allen
 * @date 2019/01/08
 */
@Entity
@Table(catalog = "bsdld")
@Cached(size = CachedSizes.DEFAULT, persister = @Persister(Persisters.PRE_HOUR))
@InitialConfig(type = InitialType.ALL)
public class ShopConfig implements IEntity<String>, PerPersist {
    @Id
    @Column(length = 5)
    private String id;
    private String name;
    private Integer consume;
    @Enumerated(EnumType.ORDINAL)
    private ConsumeType consumeType;
    @Transient
    private Map<String, Object> obtain;
    private String obtainJson;
    @Enumerated(EnumType.ORDINAL)
    private ShopModule shopModule;
    private Integer buyLimit;
    private Boolean isValid;

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getConsume() {
        return consume;
    }

    public void setConsume(Integer consume) {
        this.consume = consume;
    }

    public ConsumeType getConsumeType() {
        return consumeType;
    }

    public void setConsumeType(ConsumeType consumeType) {
        this.consumeType = consumeType;
    }

    public Map<String, Object> getObtain() {
        return obtain;
    }

    public void setObtain(Map<String, Object> obtain) {
        this.obtain = obtain;
    }

    public String getObtainJson() {
        return obtainJson;
    }

    public void setObtainJson(String obtainJson) {
        this.obtainJson = obtainJson;
    }

    public ShopModule getShopModule() {
        return shopModule;
    }

    public void setShopModule(ShopModule shopModule) {
        this.shopModule = shopModule;
    }

    public Integer getBuyLimit() {
        return buyLimit;
    }

    public void setBuyLimit(Integer buyLimit) {
        this.buyLimit = buyLimit;
    }

    public Boolean getValid() {
        return isValid;
    }

    public void setValid(Boolean valid) {
        isValid = valid;
    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public void afterLoad() {
        this.obtain = JsonUtils.string2Map(this.obtainJson, String.class, Object.class);
    }

    @Override
    public void beforePersist() {
        this.obtainJson = JsonUtils.object2String(this.obtain);
    }
}
